import { primaryColor, tableRowHoverBg } from "../src/defaultSettings";
import pageRoutes from "./router.config";
import getParams from "./params";
import webpackPlugin from "./plugin.config";

// 是否打印请求参数信息
export const logRequest = true;

// 获取命令参数
const { publicPath: originalPathPrefix = "" } = getParams();
/**
 * 组合修正路径，前后以/包裹，实现绝对路径
 * 例如：
 * 参数值为vvweb，修正后为/vvweb/
 * 参数值为http://domain.com/vvweb，修正后为http://domain.com/vvweb/
 */
const publicPath = originalPathPrefix
  ? `/${originalPathPrefix}/`
      .replace(/\/+/g, "/")
      .replace(/^\/(https?:\/)(.*)$/, "$1/$2")
  : "";

const isProd = process.env.ENV === "production";
const isTest = process.env.ENV === "test";
const isOnline = isProd || isTest;

export default {
  esbuild: {},
  antd: {}, // 动态引入,与external冲突
  webpack5: {},
  mfsu: {},
  dva: {
    disableModelsReExport: true,
    lazyLoad: true,
    hmr: true,
    skipModelValidate: true,
  },
  locale: {
    default: "zh-CN", // default zh-CN
    baseNavigator: false, // default true, when it is true, will use `navigator.language` overwrite default
  },
  dynamicImport: {
    loading: "@/components/PageLoading/index",
  },
  nodeModulesTransform: { type: "none" },
  moveMock: false,
  moveService: false,
  // modifyRequest: true,
  // autoAddMenu: true
  // targets: {
  //     ie: 11
  // },
  // minimizer: 'terserjs',
  // mfsu: {},
  fastRefresh: {},
  hash: true,
  /**
   * 路由相关配置
   */
  routes: pageRoutes,

  /**
   * webpack 相关配置
   */
  treeShaking: isOnline,
  extraBabelPlugins: [
    "@babel/plugin-proposal-nullish-coalescing-operator",
    "@babel/plugin-proposal-optional-chaining",
    [
      "import",
      {
        libraryName: "antd-mobile",
        style: true,
      },
    ],
  ].concat(
    isProd ? [["transform-remove-console", { exclude: ["error", "warn"] }]] : []
  ),
  define: {
    APP_TYPE: process.env.APP_TYPE || "",
    ENV: process.env.ENV || "",
  },
  // Theme for antd
  // https://ant.design/docs/react/customize-theme-cn
  theme: {
    "primary-color": primaryColor,
    "form-item-margin-bottom": "24px", // 这个保持不变，不然样式会有问题的
    "card-padding-base": "16px",
    "card-radius": "4px",
    "card-head-padding": "12px",
    "modal-body-padding": "12px",
    "layout-header-background": "#1E1E1E",
    "layout-header-height": "56px",
    "brand-primary": primaryColor,
    "brand-primary-tap": primaryColor,
    "table-row-hover-bg": tableRowHoverBg,
  },
  // ...(isOnline ? { chainWebpack: webpackPlugin } : {}),
  // devtool: isOnline ? false : "cheap-module-source-map",
  devtool: false,
  ignoreMomentLocale: true,
  lessLoader: {
    javascriptEnabled: true,
  },
  chainWebpack: (config) => {
    if (process.env.NODE_ENV === "development") {
      config.module
        .rule("js-in-node_modules")
        .exclude.add(/node_modules/)
        .end();
      config.module
        .rule("ts-in-node_modules")
        .exclude.add(/node_modules/)
        .end();
    }
  },
  ...(isProd ? { publicPath } : {}),
};
